<template>
  <basic-container>
    <div>
      <m-s-search
        @searchClick="
          (v) => {
            (queryForm = v), getList();
          }
        "
        :list="globalMethodOption.column"
      >
      </m-s-search>
    </div>

    <avue-crud
      :option="globalMethodOption"
      :data="data"
      :table-loading="loading"
      :page.sync="page"
      @current-change="currentChange"
      @size-change="sizeChange"
      @selection-change="(v) => (selection = v)"
      ref="curd"
    >
      <template slot="menuLeft">
        <el-button
          size="small"
          type="primary"
          @click="
            addToTabsList('全局方法', { oid: -1 }, 'globalMethodTemp', 'open')
          "
          >新增
        </el-button>
      </template>

      <template slot="menu" slot-scope="{ row }">
        <el-button
          type="text"
          @click="addToTabsList('全局方法', row, 'globalMethodTemp', 'view')"
        >
          查看详情
        </el-button>
        <el-button
          type="text"
          :disabled="row.isAuto === 0"
          @click="addToTabsList('全局方法', row, 'globalMethodTemp', 'edit')"
        >
          修改
        </el-button>
        <el-button type="text" :disabled="row.isAuto === 0" @click="delRow(row)"> 删除 </el-button>
      </template>
    </avue-crud>
    <ms-drawer
      @refresh="getList()"
      :visible.sync="visible"
      :tab-list.sync="tabsList"
    ></ms-drawer>
  </basic-container>
</template>

<script>
import MSSearch from "@/DMDComponents/MSSearch.vue";
import * as taskManageApi from "@/api/taskManage/taskManage";

export default {
  name: "globalMethod",
  components: {
    MSSearch,
  },
  inject: ["globalMethodOption"],

  data() {
    return {
      data: [],
      queryForm: {},
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 1,
      },
      visible: false,
      loading: false,
      tabsList: [],
    };
  },
  methods: {
    // 向抽屉中添加tab页 根据 oid 去重
    addToTabsList(title, row, component, type) {
      let ids = this.tabsList.map((item) => item.row[this.idKey]);
      if (!ids.includes(row[this.idKey])) {
        this.tabsList.push({ title, row, component, type });
      }
      this.visible = true;
    },
    getList() {
      const params = {};
      for(let key in this.queryForm){
        if(this.queryForm[key]){
          params[key] = this.queryForm[key]
        }
      }
      taskManageApi.getMethodList({...params,start: this.page.currentPage,length:this.page.pageSize}).then((res) => {
        this.data = res.data.data;
        this.page.total = res.data.recordsTotal;
      });
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.getList();
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.getList();
    },
       // 删除
    delRow(data) {
      this.$confirm("是否删除？").then(() => {
        taskManageApi.delMethod({ id: data.id }).then(() => {
          this.$message.success("删除成功");
          this.getList();
        });
      });
    },
  },
  mounted(){
    this.getList();
  }
};
</script>
